#include"Mutex.hpp"
#include"sem.hpp"
#include<vector>


using namespace  semnamespace;
using namespace MutexNamespace;


const int capcount=5;

template<typename  T>
class RingQueue
{
    public:
    RingQueue(int cap=capcount)
    :_arr(cap),
    _cap(cap),
    _in_sem(cap),
    _out_sem(0),
    _front(0),
    _back(0)
    {

    }

    void EQueue(const T& x)
    {
        _in_sem.P();
        {
            LockGuard lock(_in_mutex);
            _arr[_front]=x;
            ++_front;
            _front%=_cap;
        }
        _out_sem.V();

    }

    T Pop()
    {
        T ret;
        _out_sem.P();
        {
            LockGuard lock(_out_mutex);
            ret=_arr[_back];
            ++_back;
            _back%=_cap;

        }
        _in_sem.V();

        return ret;

    }

    ~RingQueue()
    {
    
    }



    private:
    std::vector<T> _arr;
    int _cap;
    Sem _in_sem;
    int _front;
    Sem _out_sem;
    int _back;

    Mutex _in_mutex;
    Mutex _out_mutex;


};